package com.so.studyonline.controller;

import com.auth0.jwt.JWT;
import com.so.studyonline.annotation.PassToken;
import com.so.studyonline.annotation.UserLoginToken;
import com.so.studyonline.mapper.*;
import com.so.studyonline.model.SoAllContent;
import com.so.studyonline.model.SoUser;
import com.so.studyonline.service.SoUserService;
import com.so.studyonline.utils.ResMessage;
import com.so.studyonline.model.SoArticle;
import com.so.studyonline.model.SoMenulist;
import com.so.studyonline.mapper.SoArticleMapper;
import com.so.studyonline.mapper.SoMenulistMapper;
import com.so.studyonline.utils.ResMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("api")
public class GetInformationController {

    private SoMenulistMapper soMenulistMapper;
    private SoArticleMapper soArticleMapper;
    private SoAllContentMapper soAllContentMapper;

    @Autowired
    public void setSoMenulistMapper(SoMenulistMapper soMenulistMapper) {
        this.soMenulistMapper = soMenulistMapper;
    }

    @Autowired
    public void setSoArticleMapper(SoArticleMapper soArticleMapper) {
        this.soArticleMapper = soArticleMapper;
    }

    @Autowired
    public void setSoAllContentMapper(SoAllContentMapper soAllContentMapper) {
        this.soAllContentMapper = soAllContentMapper;
    }

    private SoUserService soUserService;
    @Autowired
    public void setSoUserService(SoUserService soUserService){
        this.soUserService = soUserService;
    }

    /**
     * @param request 请求头
     * @return 菜单列表
     * @apiNote getMenuList && 查询菜单
     */
    @UserLoginToken
    @RequestMapping("menu")
    @ResponseBody
    public String getMenuList(HttpServletRequest request) {
        // 从 http 请求头中取出 token(Authorization)
        String token = request.getHeader("Authorization");
        String userNo = JWT.decode(token).getAudience().get(0);
        SoUser user = new SoUser();
        user.setUserNo(userNo);
        SoUser soUser = soUserService.findByUserInfo(user);

        ResMessage resMessage = new ResMessage();
//        String[] userInfo = String.valueOf(request.getSession().getAttribute("userInfo")).split(",");
        SoMenulist soMenulist = new SoMenulist();
        soMenulist.setUserRole(soUser.getUserRole());
        List<SoMenulist> soMenulists = soMenulistMapper.getUserMenu(soMenulist);
        List<SoMenulist> soMenulistsTwo = new ArrayList<>();  // 二级菜单
        for (int i = 0, length = soMenulists.size(); i < length; i++) {
            SoMenulist soMenulist1 = soMenulists.get(i);
            if (soMenulist1.getMenuLevel() == 2) {
                soMenulistsTwo.add(soMenulist1);
                soMenulists.remove(soMenulist1);
                length--;
                i--;
            }
        }
        List<Map<String, Object>> resLists = new ArrayList<>();
        for (SoMenulist menulist : soMenulists) {
            Map<String, Object> resItem = new HashMap<>();
            resItem.put("meunname", menulist.getMenuName());
            resItem.put("menuvalue", menulist.getMenuKey());
            List<Map<String, Object>> resListsSon = new ArrayList<>();
            for (int j = 0, lengthj = soMenulistsTwo.size(); j < lengthj; j++) {
                Map<String, Object> resItemSon = new HashMap<>();
                SoMenulist soMenulist2 = soMenulistsTwo.get(j);
                if (soMenulist2.getParentMent() == menulist.getId()) {
                    resItemSon.put("meunname", soMenulist2.getMenuName());
                    resItemSon.put("menuvalue", soMenulist2.getMenuKey());
                    resListsSon.add(resItemSon);
                    soMenulistsTwo.remove(soMenulist2);
                    lengthj--;
                    j--;
                }
            }
            resItem.put("menusonlist", resListsSon);
            resLists.add(resItem);
        }
        return resMessage.sysSuccess(resLists);
    }

    /**
     * @param soArticle 编辑的信息
     * @return 返回结果
     * @apiNote articleEdit && 编辑文章
     */
    @UserLoginToken
    @RequestMapping("article_edit")
    @ResponseBody
    public String articleEdit(@RequestBody SoArticle soArticle) {
        ResMessage resMessage = new ResMessage();
        log.info("article-id:{}", soArticle.getArticleId());
        SoAllContent soAllContent = new SoAllContent();
        soAllContent.setId(soArticle.getArticleId());
        soAllContent.setTitle(soArticle.getTitle());
        try {
            soArticleMapper.updateArticle(soArticle);
            soAllContentMapper.updateContentItem(soAllContent);
            return resMessage.sysSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return resMessage.sysError();
        }
    }
}
